<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\User;
use App\Http\Model\UserModel;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class IndexController extends CommonController
{
    //
    public function index()
    {
        return view('admin.index');
    }

    public function info(){
        return view('admin.info');
    }

    public function pass(){

        if($input = Input::all()){

            $rules = [
                'password'=>'required|between:6,20|confirmed',
            ];
            $msg = [
                'password.required'=>'new password can not be empty',
                'password.between'=>'length of new password must between 6 digits to 20 digits',
                'password.confirmed'=>'new password must be confirmed',
            ];
            $vali = Validator::make($input,$rules,$msg);
            if ($vali->passes()) {
                $user = UserModel::first();
                $_password = Crypt::decrypt($user->user_pass);
                if($input['password_o']==$_password){
                    $user->user_pass = Crypt::encrypt($input['password']);
                    $user->update();

                    return back()->with('errors','密码修改成功！');
                }else{
                    return back()->with('errors','原密码错误！');
                }
            }else{
                return back()->withErrors($vali);
            }
        }else{
            return view('admin.pass');
        }

    }
}
